# coding=utf8
import sys
import random
from datetime import datetime
import matplotlib, tushare

import matplotlib.ticker as ticker

matplotlib.use("Qt5Agg")
import matplotlib.pyplot as plt
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget

from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

global real_data
real_data = [20]


class MyMplCanvas(FigureCanvas):
    """这是一个窗口部件，即QWidget（当然也是FigureCanvasAgg）"""

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        fig, self.axes = plt.subplots()
        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass


class MyStaticMplCanvas(MyMplCanvas):
    """静态画布"""

    def compute_initial_figure(self):
        a = tushare.get_hist_data('000002', start='2017-05-22', end='2017-05-31')
        t = a.open
        s = [datetime.strptime(d, '%Y-%m-%d').date() for d in a.index]
        self.axes.plot(s, t)


class MyDynamicMplCanvas(MyMplCanvas):
    """动态画布：每1秒自动更新"""

    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.update_figure)
        timer.start(1000)

    def compute_initial_figure(self):
        self.axes.plot(real_data, 'r')

    def update_figure(self):
        global real_data
        # 构建4个随机整数，位于闭区间[0, 10]
        real_data.append(tushare.get_realtime_quotes('000581').price[0])
        self.axes.set_xlim(0, 3600)

        self.axes.plot(real_data, 'r')
        self.draw()


class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("程序主窗口")

        self.file_menu = QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
                                 QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)

        self.help_menu = QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)

        self.help_menu.addAction('&About', self.about)

        self.main_widget = QWidget(self)

        l = QVBoxLayout(self.main_widget)
        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(sc)
        l.addWidget(dc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)
        # 状态条显示2秒
        self.statusBar().showMessage("matplotlib 万岁!", 2000)

    def fileQuit(self):
        self.close()

    def closeEvent(self, ce):
        self.fileQuit()

    def about(self):
        QMessageBox.about(self, "About",
                          """embedding_in_qt5.py example
                          Copyright 2015 BoxControL
                  
                          This program is a simple example of a Qt5 application embedding matplotlib
                          canvases. It is base on example from matplolib documentation, and initially was
                          developed from Florent Rougon and Darren Dale.
                  
                          http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html
                  
                          It may be used and modified with no restriction; raw copies as well as
                          modified versions may be distributed without limitation.
                          """
                          )


if __name__ == '__main__':
    app = QApplication(sys.argv)

    aw = ApplicationWindow()
    aw.setWindowTitle("PyQt5 与 Matplotlib 例子")
    aw.show()
    # sys.exit(qApp.exec_())
    app.exec_()